import {
  SETREADTSTATE,
  DELMSG,
  SETALLREAD
} from './actionTypes.js'
const defaultState = {
  msgLists: [{
      id: 1,
      title: '我是消息1',
      content: "消息1的内容",
      hasRead: false,
      avatar: 'https://himg.bdimg.com/sys/portraitn/item/d1846c697869616e676d616769638c27'
    },
    {
      id: 2,
      title: '我是消息2',
      content: "消息2的内容",
      hasRead: true,
      avatar: 'https://himg.bdimg.com/sys/portraitn/item/d1846c697869616e676d616769638c27'
    },
    {
      id: 3,
      title: '我是消息3',
      content: "消息3的内容",
      hasRead: false,
      avatar: 'https://himg.bdimg.com/sys/portraitn/item/d1846c697869616e676d616769638c27'
    },
    {
      id: 4,
      title: '我是消息4',
      content: "消息4的内容",
      hasRead: false,
      avatar: 'https://himg.bdimg.com/sys/portraitn/item/d1846c697869616e676d616769638c27'
    }
  ]
}


const reducer = (state = defaultState, action )=>{
   const newState = JSON.parse(JSON.stringify(state));
   switch(action.type){
     case SETREADTSTATE:  // 设置消息已读状态
      newState.msgLists[action.index].hasRead = true
      break;
     case DELMSG: 
      newState.msgLists.splice(action.index, 1)
      break;
     case SETALLREAD: 
       newState.msgLists.forEach((msg)=>{ msg.hasRead = true });
      default: break;
   }
   return newState;
}


export default reducer